import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';

/// FileName assets_image
///
/// @Author lax
/// @Date 2023/5/11
///
/// @Description

class AssetsImage extends StatelessWidget {
  static String assetsDirectory = 'assets';

  static Color? defaultBgColor;

  static String? defaultPath;

  final double? width;

  final double? height;

  final double? size;

  final double? radius;

  final String? url;

  late final String? path;

  final String? name;

  final String? package;

  late final Color? bgColor;

  final Color? color;

  final BoxFit? fit;

  final Rect? centerSlice;

  final EdgeInsets? margin;

  final EdgeInsets? padding;

  AssetsImage({
    super.key,
    this.width,
    this.height,
    this.size,
    this.radius,
    this.url,
    String? path,
    this.name,
    this.package,
    Color? bgColor,
    this.color,
    this.fit = BoxFit.contain,
    this.centerSlice,
    this.margin,
    this.padding,
  }) {
    this.bgColor = bgColor ?? defaultBgColor;
    this.path = path ?? defaultPath;
  }

  @override
  Widget build(BuildContext context) {
    if (margin == null && radius == null) {
      return body();
    }
    return Padding(
      padding: margin ?? EdgeInsets.zero,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(radius ?? 0),
        child: body(),
      ),
    );
  }

  Widget body() {
    if (bgColor == null && padding == null) {
      return image();
    }
    return Container(
      color: bgColor,
      padding: padding,
      child: image(),
    );
  }

  Widget image() {
    double? w;
    double? h;
    if ((width != null && height != null) || size != null) {
      final pad = padding ?? EdgeInsets.zero;
      w = max((width ?? size ?? 0) - pad.left - pad.right, 0);
      h = max((height ?? size ?? 0) - pad.top - pad.bottom, 0);
    }
    if (url != null && url != '') {
      // return SizedBox(width: w, height: h);
      final str = url ?? '';
      return CachedNetworkImage(
        imageUrl: str,
        width: w,
        height: h,
        color: color,
        fit: fit,
      );
      // return Image.network(
      //   str,
      //   width: w,
      //   height: h,
      //   color: color,
      //   fit: fit,
      // );
    }
    if (name != null && name != '') {
      final str = imagePath(path: path, name: name);
      return Image.asset(
        str ?? '',
        package: package,
        width: w,
        height: h,
        color: color,
        fit: fit,
        centerSlice: centerSlice,
      );
    }
    return SizedBox(width: w, height: h);
  }

  /// 图片路径
  static String? imagePath({String? path, String? name}) {
    if (name == null) {
      return null;
    }
    final prefix = name.startsWith(assetsDirectory) ? '' : '$assetsDirectory/';
    final suffix = name.contains('.') ? '' : '.png';
    if (path == null || path == '') {
      return '$prefix$name$suffix';
    }
    return '$prefix$path/$name$suffix';
  }

  /// 背景图 默认填充满宽高
  static Decoration? decoration({
    String? path,
    String? name,
    String? url,
    BoxFit? fit,
    Alignment? alignment,
    Color? color,
    List<BoxShadow>? boxShadow,
    Gradient? gradient,
    Rect? centerSlice,
  }) {
    return BoxDecoration(
      image: decorationImage(
        path: path,
        name: name,
        url: url,
        fit: fit,
        alignment: alignment,
        centerSlice: centerSlice,
      ),
      color: color,
      boxShadow: boxShadow,
      gradient: gradient,
    );
  }

  static DecorationImage? decorationImage({
    String? path,
    String? name,
    String? url,
    BoxFit? fit,
    Alignment? alignment,
    Rect? centerSlice,
  }) {
    if (name == null && url == null) {
      return null;
    }
    if (url != null) {
      return DecorationImage(
        image: NetworkImage(url),
        fit: fit,
        alignment: alignment ?? Alignment.center,
        centerSlice: centerSlice,
      );
    }
    path ??= defaultPath;
    return DecorationImage(
      image: AssetImage(imagePath(path: path, name: name) ?? ''),
      fit: fit,
      alignment: alignment ?? Alignment.center,
      centerSlice: centerSlice,
    );
  }
}
